package com.mocoop.photo;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;






import java.util.ArrayList;
import java.util.List;

import cn.imeth.android.lang.utils.Androids;

import com.mocoop.jfqc.R;

/**
 * Created by 火蚁 on 15/4/29.
 */
public class PhotoBrowseActivity extends FragmentActivity implements View.OnClickListener {

    public static final String BUNDLE_KEY_IMAGES = "bundle_key_images";
    public static final String BUNDLE_KEY_INDEX = "bundle_key_index";

    ViewPager viewpager;
    TextView tvPhotoIndex;


    private PhotoAdapter adapter;

    private String[] imageUrls;

    private int index = 0;

    public static void startActivity(Context context, int index, String... images) {

        index = index < 0 ? 0 : index;
        index = index >= images.length ? images.length : index;

        Intent intent = new Intent();
        intent.setClass(context,PhotoBrowseActivity.class);
        intent.putExtra(BUNDLE_KEY_INDEX, index);
        intent.putExtra(BUNDLE_KEY_IMAGES, images);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_browse);

        initView();
    }

    private void initView() {

        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tvPhotoIndex = (TextView) findViewById(R.id.tv_photo_index);
     

        setOnClickListener(R.id.ll_download);
        setOnClickListener(R.id.tv_photo_index);

        adapter = new PhotoAdapter(getSupportFragmentManager());

        viewpager.setAdapter(adapter);

        Intent intent = getIntent();
        if (intent != null) {
            index = intent.getIntExtra(BUNDLE_KEY_INDEX, 0);
            imageUrls = intent.getStringArrayExtra(BUNDLE_KEY_IMAGES);
        }
        if(imageUrls==null){
        	Androids.makeText("请传入图片!");
        	finish();
        	return;
        }
        
        setIndex();
        adapter.add(imageUrls);
        viewpager.setCurrentItem(index);
        viewpager.setOffscreenPageLimit(1);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
                setIndex();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //TypefaceUtils.setSemantic(tvDownloadIcon);
    }

    private void setIndex() {
    	if(imageUrls!=null)
        tvPhotoIndex.setText((index+1)  + "/" + imageUrls.length);
    }

    private void setOnClickListener(int id) {
        findViewById(id).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoBrowseActivity.this.onClick(v);
            }
        });
    }

    public void onClick(View v) {
//        saveImageToGallery();
    }

    class PhotoAdapter extends FragmentStatePagerAdapter {

        private List<PhotoFragment> fragments;

        public PhotoAdapter(FragmentManager fm) {
            super(fm);
            fragments = new ArrayList<PhotoFragment>();
        }

        @Override
        public PhotoFragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        public void add(String[] imageUrls) {
           if(imageUrls==null)return;
        	for (String url : imageUrls) {
                fragments.add(PhotoFragment.newInstance(url));
            }
            notifyDataSetChanged();
        }
    }  
        
    
}
